/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <gtest/gtest.h>

#include <internal/bus/bus_i2c_mux/i2c_mux.h>
#include <internal/hasdef.h>
#include <mc/dict.h>

namespace {

class bus_i2c_mux_test : public ::testing::Test {
protected:
    dev::bus_i2c_mux m_bus;
};

// 校验初始化流程能够读取 ChannelId
TEST_F(bus_i2c_mux_test, init_with_channel_id) {
    mc::mutable_dict csr_object;
    csr_object["ChannelId"] = static_cast<uint8_t>(5);

    EXPECT_TRUE(m_bus.init(csr_object));
    EXPECT_EQ(m_bus.get_id(), static_cast<uint8_t>(5));
}

// 未提供 ChannelId 时应保持默认值
TEST_F(bus_i2c_mux_test, init_without_channel_id) {
    mc::mutable_dict csr_object;
    EXPECT_TRUE(m_bus.init(csr_object));
    EXPECT_EQ(m_bus.get_id(), static_cast<uint8_t>(0));
}

// 无法转换的 ChannelId 应导致初始化失败
TEST_F(bus_i2c_mux_test, init_with_invalid_channel_id) {
    mc::mutable_dict csr_object;
    csr_object["ChannelId"] = std::string("invalid");

    EXPECT_FALSE(m_bus.init(csr_object));
    EXPECT_EQ(m_bus.get_id(), static_cast<uint8_t>(0));
}

// read 接口当前不支持，期望抛出异常
TEST_F(bus_i2c_mux_test, read_not_supported) {
    ACCESS_DATA_S data{};
    EXPECT_THROW(m_bus.read(data), mc::invalid_op_exception);
}

// write 接口当前不支持，期望抛出异常
TEST_F(bus_i2c_mux_test, write_not_supported) {
    ACCESS_DATA_S data{};
    EXPECT_THROW(m_bus.write(data), mc::invalid_op_exception);
}

// 锁接口不支持，需抛出异常
TEST_F(bus_i2c_mux_test, lock_not_supported) {
    EXPECT_THROW(m_bus.bus_lock(), mc::invalid_op_exception);
    EXPECT_THROW(m_bus.bus_unlock(), mc::invalid_op_exception);
}

} // namespace
